< previous page page_112 next page >

Page 112
9f31265bb40b592b1929e18a47ab28e5.gif
TIP
Keep the scope of variables limited and avoid global variables if at all possible.
Rules of Scope
Because there are different locations in which you can declare variables, you need to know where to access the different variables. Table 6.3 shows how and where you declare a variable; it also provides a sample name, the scope, and the lifetime of that variable.
Table 6.3 Declaration of Variables.
DeclarationLocationExampleScopeLifetime
GlobalBAS filegstrNameApplicationApplication
PublicBAS filegstrNameApplicationApplication
PrivateModulemstrNameModuleModule
DimModulemstrNameModuleModule
PublicForm/classFirstNameForm/ClassModule
DimProcedurestrNameProcedureProcedure

In this table, you can see that Global and Public are synonymous when either is declared in a standard module. Private and Dim are synonymous when either is declared in any type of module. A public variable declared in a module can only be used when its referenced with that modules name, such as <modName>.<variableName>. A Dim statement is the only declaration you can use to declare local variables within a procedure.
Public Variables in Standard Modules
Publicly scoped variables must be created in a standard module. To create a standard module, you need to select Project, Add Module from the Visual Basic menu. This inserts a code module into your project. These code modules are where you place all Public variables and Public procedures (which well cover later).

 
< previous page page_112 next page >

If you like this book, buy it!